
#! /usr/bin/python

import sys
import os
import time
#sys.path.append('..'+os.sep+'trunk')
sys.path.append('..')
from spade import *
class Example(Agent.Agent):
    def _setup(self):
        entry = "Starting Organization Behaviour"




    def _process(self):
	 pass
class MyOrganization(Organization.Coalition):
    def _process(self):
        print self.getOwner()
    	u=MyUnit(agent=self.myAgent, nick="jefe", name="unit1",password="pass")
    	self.addUnit(u)
        u=MyUnit(agent=self.myAgent, nick="jefe", name="unit2",password="pass")
        self.addUnit(u)
        time.sleep(500)
        self.destroy()

    def leadershipTest(self,sender,desc=None):
      return True

class MyUnit(Unit.Team):
    def _process(self):
        self.setMaxAgents(5)
        self.setMinAgents(1)
        time.sleep(50)
        #self.setMinAgents(2)
        """
       # mens=ACLMessage.ACLMessage()
        mens.setPerformative("request")
        mens.setSender(self.myAgent.getAID())
        aid=AID.aid("pepe@perez",["xmpp://pepe@perez"])
        mens.addReceiver(aid)
        mens.setContent("Trabajo??")
        time.sleep(30)
        print "Solicitamos"+str(mens)
        m=self.getMemberList()
        print m
        for i in m:
            self.sendPrivateMessage(i,mens)
            #self.sendMessage(mens)
        """
        pass


    def creationTest(self,sender,desc=None):
       d=desc
       if d.has_key("mision"):
           if str(d["mision"])=="supervision":
               return True
       return False

    def entryUnitTest(self,sender,desc=None):
      return True

    def leadershipTest(self,sender,desc=None):
      return True


if __name__ == "__main__":
    agent = "example@apolo.dsic.upv.es"
    #agent = "agent@thx1138.dsic.upv.es"
    #agent = "agent@tatooine.dsic.upv.es"

    b = Example(agent,"pasword")
    b.start()
    print "Agent "+agent+" registering"
    o=MyOrganization(agent=b, nick="jefe", name="supervision",password="pass")
    b.createOrganization(o)
    time.sleep(100)
    b._shutdown()



